﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Transactions;

namespace Clipper.DAL
{
    /// <summary>
    /// 数据访问类:SampleDetectItems
    /// </summary>
    public partial class SampleDetectItems
    {
        public SampleDetectItems()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long SampleID, string DetectItem)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SampleDetectItems");
            strSql.Append(" where SampleID=@SampleID and DetectItem=@DetectItem ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8),
                    new SQLiteParameter("@DetectItem", DbType.String,20)            };
            parameters[0].Value = SampleID;
            parameters[1].Value = DetectItem;

            return SQLiteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SampleDetectItems model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SampleDetectItems(");
            strSql.Append("SampleID,DetectItem)");
            strSql.Append(" values (");
            strSql.Append("@SampleID,@DetectItem)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8),
                    new SQLiteParameter("@DetectItem", DbType.String,20)};
            parameters[0].Value = model.SampleID;
            parameters[1].Value = model.DetectItem;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool BatchAdd(List<Model.SampleDetectItems> models)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (SQLiteConnection connection = new SQLiteConnection(SQLiteHelper.connectionString))
                {
                    connection.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(connection))
                    {
                        cmd.CommandText = @"insert into SampleDetectItems(SampleID,DetectItem)  values (@SampleID,@DetectItem) ";
                        foreach (Model.SampleDetectItems model in models)
                        {
                            //        StringBuilder strSql = new StringBuilder();
                            //        strSql.Append("");
                            //        strSql.Append("");
                            //        strSql.Append(" values (");
                            //        strSql.Append("");
                            SQLiteParameter[] parameters = {
                            new SQLiteParameter("@SampleID", DbType.Int64,8),
                            new SQLiteParameter("@DetectItem", DbType.String,20)};
                            parameters[0].Value = model.SampleID;
                            parameters[1].Value = model.DetectItem;
                            cmd.Parameters.Add(parameters[0]);
                            cmd.Parameters.Add(parameters[1]);

                            try
                            {
                                //SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
                                int rows = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            catch (System.Data.Common.DbException)
                            {
                                return false;
                            }
                        }//end foreach
                    }//end command
                }//end connection
                scope.Complete();

            }// end trans
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SampleDetectItems model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SampleDetectItems set ");
            strSql.Append("SampleID=@SampleID,");
            strSql.Append("DetectItem=@DetectItem");
            strSql.Append(" where SampleID=@SampleID and DetectItem=@DetectItem ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8),
                    new SQLiteParameter("@DetectItem", DbType.String,20)};
            parameters[0].Value = model.SampleID;
            parameters[1].Value = model.DetectItem;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long SampleID, string DetectItem)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SampleDetectItems ");
            strSql.Append(" where SampleID=@SampleID and DetectItem=@DetectItem ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8),
                    new SQLiteParameter("@DetectItem", DbType.String,20)            };
            parameters[0].Value = SampleID;
            parameters[1].Value = DetectItem;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除样本的检测项
        /// </summary>
        public bool DeleteDetectItems(long SampleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SampleDetectItems ");
            strSql.Append(" where SampleID=@SampleID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8)        };
            parameters[0].Value = SampleID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SampleDetectItems GetModel(long SampleID, string DetectItem)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SampleID,DetectItem from SampleDetectItems ");
            strSql.Append(" where SampleID=@SampleID and DetectItem=@DetectItem ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SampleID", DbType.Int64,8),
                    new SQLiteParameter("@DetectItem", DbType.String,20)            };
            parameters[0].Value = SampleID;
            parameters[1].Value = DetectItem;

            Model.SampleDetectItems model = new Model.SampleDetectItems();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SampleDetectItems DataRowToModel(DataRow row)
        {
            Model.SampleDetectItems model = new Model.SampleDetectItems();
            if (row != null)
            {
                if (row["SampleID"] != null && row["SampleID"].ToString() != "")
                {
                    model.SampleID = long.Parse(row["SampleID"].ToString());
                }
                if (row["DetectItem"] != null)
                {
                    model.DetectItem = row["DetectItem"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SampleID,DetectItem ");
            strSql.Append(" FROM SampleDetectItems ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SampleDetectItems ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SQLiteHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.DetectItem desc");
            }
            strSql.Append(")AS Row, T.*  from SampleDetectItems T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return SQLiteHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "SampleDetectItems";
            parameters[1].Value = "DetectItem";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
}
        */
        #endregion
    }
}